package com.hongfeng.support.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.AutoKnowledge;
import com.hongfeng.support.service.AutoKnowledgeService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 
 * <汽车小知识Controller>
 * 
 * @author llc
 * @date 创建时间：2012-12-5 下午02:26:49
 */
@Controller
@Online
public class AutoKnowledgeController extends BaseController {

	@Resource
	private AutoKnowledgeService autoKnowledgeService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String title, Page<AutoKnowledge> page) {
		log.info("标题：" + title);
		ParmItem item = new ParmItem("title", title, FieldOperator.LIKE);
		autoKnowledgeService.findPageByQuery(page, AutoKnowledge.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("autoKnowledge") String autoKnowledgeJson) {
		log.info("添加汽车小知识:"
				+ (autoKnowledgeJson != null ? autoKnowledgeJson : null));
		if (autoKnowledgeJson == null) {
			this.sendErrorResponse(response, "介绍不能为空。");
			return;
		}
		AutoKnowledge autoKnowledge = (AutoKnowledge) JsonUtils.json2Object(
				autoKnowledgeJson, AutoKnowledge.class);
		autoKnowledgeService.create(autoKnowledge);
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("autoKnowledge") String autoKnowledgeJson) {
		log.info("修改汽车小知识:"
				+ (autoKnowledgeJson != null ? autoKnowledgeJson : null));
		if (autoKnowledgeJson == null) {
			this.sendErrorResponse(response, "汽车小知识信息不能为空。");
			return;
		}
		AutoKnowledge autoKnowledge = (AutoKnowledge) JsonUtils.json2Object(
				autoKnowledgeJson, AutoKnowledge.class);
		AutoKnowledge qAutoKnowledge = autoKnowledgeService.read(autoKnowledge
				.getId());
		autoKnowledge.setCreateTime(qAutoKnowledge.getCreateTime());
		autoKnowledge.setCreator(qAutoKnowledge.getCreator());
		autoKnowledgeService.update(autoKnowledge);
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("读取汽车小知识:" + id);
		AutoKnowledge knowledge = autoKnowledgeService.read(id);
		String json = JsonUtils.bean2Json(knowledge);
		log.info("汽车小知识信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除汽车小知识:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的汽车小知识。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			autoKnowledgeService.delete(Long.parseLong(id));
		}
	}
}
